package org.wishwork.api.inventory.query;

import cn.hutool.core.util.StrUtil;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.ToString;
import org.wishwork.api.ExtendBase;
import org.wishwork.exception.ExceptionEnum;
import org.wishwork.exception.JSTException;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;

/**
 * @description: 库存查询API
 * @author: WangYX
 * @create: 2022-04-06 11:08
 * @Version: 1.0.0
 **/
@EqualsAndHashCode
@ToString
@Getter
public class InventoryQueryRequest extends ExtendBase {


    /**
     * 非必填
     * 分仓公司编号，值不传或为0查询所有仓的总库存，其它为指定仓的库存
     */
    private Integer wmsCoId;

    /**
     * 非必填
     * 商品编码,多个用逗号分隔，最多20，与修改时间不能同时为空
     */
    private String skuIds;

    private InventoryQueryRequest(Integer wmsCoId,
                                  String skuIds,
                                  Integer pageIndex,
                                  Integer pageSize,
                                  String modifiedBegin,
                                  String modifiedEnd) {
        super(pageIndex, pageSize, modifiedBegin, modifiedEnd);
        this.wmsCoId = wmsCoId;
        this.skuIds = skuIds;
    }

    public static Builder builder() {
        return new Builder();
    }

    @Override
    public Builder toBuilder() {
        return new Builder()
                .wmsCoId(this.wmsCoId)
                .skuIds(this.skuIds)
                .pageIndex(this.pageIndex)
                .pageSize(this.pageSize)
                .modifiedBegin(this.modifiedBegin)
                .modifiedEnd(this.modifiedEnd);
    }

    public static class Builder extends ExtendBase.Builder<Builder, InventoryQueryRequest> {

        private Integer wmsCoId;
        private String skuIds;

        public Builder wmsCoId(Integer wmsCoId) {
            this.wmsCoId = wmsCoId;
            return this;
        }

        public Builder skuIds(String skuIds) {
            if (StrUtil.isNotEmpty(this.skuIds)) {
                if (this.skuIds.split(",").length > 20) {
                    throw new JSTException(ExceptionEnum.USER_ERROR_A0420, "商品编码,多个用逗号分隔，最多20!");
                }
            }
            this.skuIds = skuIds;
            return this;
        }

        @Override
        public Builder pageIndex(Integer pageIndex) {
            this.pageIndex = pageIndex;
            return this;
        }

        @Override
        public Builder pageSize(Integer pageSize) {
            this.pageSize = pageSize;
            return this;
        }

        @Override
        public Builder modifiedBegin(String modifiedBegin) {
            this.modifiedBegin = modifiedBegin;
            return this;
        }

        @Override
        public Builder modifiedEnd(String modifiedEnd) {
            this.modifiedEnd = modifiedEnd;
            return this;
        }

        @Override
        public InventoryQueryRequest build() {
            this.pageIndex = this.pageIndex == null ? 1 : this.pageIndex < 0 ? 1 : this.pageIndex;
            this.pageSize = this.pageSize == null ? 30 : this.pageSize < 0 ? 30 : this.pageSize;
            if (StrUtil.isEmpty(this.skuIds)) {
                keyIsEmptyTimeHandle(7);
            }
            modifiedTimeHandle(7);
            return new InventoryQueryRequest(this.wmsCoId, this.skuIds, this.pageIndex, this.pageSize, this.modifiedBegin, this.modifiedEnd);
        }


    }
}
